import request from './request'
import Message from 'vue-m-message'

export function getHttp(apiRoute) {
  // const token_type = JSON.parse(localStorage.getItem('huapuauth')).token_type
  const access_token = JSON.parse(localStorage.getItem('huapuauth')).access_token
  const data = {
    // token_type,
    // access_token,
    Authorization: `Bearer ${access_token}`
  }
  const headersData = data
  return new Promise((resolve, reject) => {
    let msg
    if (apiRoute.isload !== false) {
      msg = Message.loading({
        message: apiRoute.loadMsg ? apiRoute.loadMsg : '请求中',
        align: 'center',
        showClose: false
      })
    }
    request.get(apiRoute.url, {
      params: apiRoute.data,
      headers: headersData
    })
      .then(response => {
        if (msg) { msg.close() }
        switch (response.data.code) {
          case 1:
            resolve(response.data)
            break
          case 500:
            window.postMessage({ href: 'location', url: '/#/login' }, '*')
            break
          default:
            window.postMessage({ href: 'location', url: '/#/login' }, '*')
            break
        }
      })
      .catch((error) => {
        if (msg) { msg.close() }
        Message({
          message: '请求失败,请稍后再试',
          type: 'error',
          showClose: false,
          duration: 3000,
          zIndex: 9999,
          align: 'center'
        })
        reject(error)
      })
  }
  )
}

export function postHttp(apiRoute) {
  return new Promise((resolve, reject) => {
    let msg
    if (apiRoute.isload !== false) {
      msg = Message.loading({
        message: apiRoute.loadMsg ? apiRoute.loadMsg : '请求中',
        align: 'center',
        showClose: false
      })
    }
    request.post(apiRoute.url, apiRoute.data)
      .then(response => {
        // console.log(JSON.stringify(response))
        if (msg) { msg.close() }
        // window.postMessage({ href: 'location', url: '/#/login' }, '*')
        switch (response.data.code) {
          case 0 :
            resolve(response.data)
            break
          case 1:
            // console.log(JSON.stringify(Message))
            if (apiRoute.url === '/member/checkphone') {
              resolve(response.data)
            } else {
              Message({
                message: response.data.msg,
                type: 'error',
                showClose: true,
                duration: 3000,
                zIndex: 9999,
                align: 'center'
              })
            }
            // resolve(response.data)
            break
          case 500:
            window.postMessage({ href: 'location', url: '/#/login' }, '*')
            break
          default:
            resolve(response.data)
            break
        }
      })
      .catch((error) => {
        if (msg) { msg.close() }
        Message({
          message: '请求失败,请稍后再试',
          type: 'error',
          showClose: false,
          duration: 3000,
          zIndex: 9999,
          align: 'center'
        })
        reject(error)
      })
  })
}
